<template>
    <el-container>
        <!-- 头部区域 -->
      <el-header>
          <div>
              <img src="../assets/heima.png" alt="">
              <span>电商后台管理系统</span>
          </div>
          <el-button type="info" v-on:click='loginOut'>退出</el-button>
      </el-header>
      <!-- 主体区域 -->
      <el-container>
        <!-- 侧边栏区域 -->
        <el-aside :width="isCollapse? '64px' : '200px'">
            <div class="button-show" v-on:click='toggleCollapse'>|||</div>
            <el-menu background-color="#333744" text-color="#fff"  active-text-color="#409EFF" unique-opened :collapse='isCollapse' :collapse-transition='false' router :default-active='activePath'>
                <!-- 一级导航 -->
              <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
                  <!-- 一级导航的模板 -->
                <template slot="title">
                    <!-- 图标 -->
                  <i :class="iconObj[item.id]"></i>
                    <!-- 文本信息 -->
                  <span>{{item.authName}}</span>
                </template>
                <!-- 二级导航 -->
                <el-menu-item :index=" '/' + subItem.path" v-for="subItem in item.children" :key='subItem.id' v-on:click="saveNavStatus('/' + subItem.path)">
                    <!-- 二级导航的模板 -->
                    <template slot="title">
                    <!-- 图标 -->
                    <i class="el-icon-menu"></i>
                    <!-- 文本信息 -->
                    <span>{{subItem.authName}}</span>
                </template>
                </el-menu-item>
              </el-submenu>
            </el-menu>
        </el-aside>
        <!-- 右侧主体区域 -->
        <el-main>
            <!-- 写占位符 -->
            <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>

</template>

<script>
export default {
    data() {
        return {
            // 左侧菜单权限的数据
            menuList:[],
            // 一级菜单字体图标的对象集合
            iconObj:{
                '125':'iconfont icon-user',
                '103':'iconfont icon-tijikongjian',
                '101':'iconfont icon-shangpin',
                '102':'iconfont icon-danju',
                '145':'iconfont icon-baobiao'

            },
            // 控制菜单栏是否折叠
            isCollapse:false,
            // 保存被激活的地址
            activePath:''
        };
    },
    created() {
        this.getMenuList()
        this.activePath = window.sessionStorage.getItem('activeIndex')
    },
    mounted() {

    },
    methods: {
        // 退出功能
        loginOut:function(){
            // 清空token
            window.sessionStorage.clear()
            // 编程式导航，跳转到登录页面
            this.$router.push('/login')
        },
        // 获取导航数据
        getMenuList: async function(){
            const {data:res} = await this.$http.get('menus')
            if(res.meta.status !== 200){
                return this.$message.error('获取菜单列表失败')
            }
            this.menuList = res.data
            
        },
        // 点击切换菜单栏的宽度
        toggleCollapse:function(){
            this.isCollapse = !this.isCollapse
        },
        // 保存激活项的index值
        saveNavStatus:function(activeIndex){
            window.sessionStorage.setItem('activeIndex',activeIndex)
            this.activePath = activeIndex
        }
    }
};
</script>

<style scoped lang="less">
.el-container{
    height: 100%;
}

.el-header{
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;
    > div{
        display: flex;
        align-items: center;
        span{
            margin-left: 20px;
        }
    }
}

.el-aside{
    background-color: #333744;
    .el-menu{
        border-right: none;
    }
    .button-show{
        text-align: center;
        font-size: 10px;
        line-height: 24px;
        color: #fff;
        cursor: pointer;
        letter-spacing: .2rem;
        background-color:#4a5064 ;
    }
}

.el-main{
    background-color: #eaedf1;
}

.iconfont{
    margin-right: 15px;
}
</style>
